# -*- coding: utf-8 -*-

"""
@Author: Coin
@Email: xiao_coin@foxmail.com

@Create Time: 2023-10-10 09:46:50
@Last Modified Time: 2023-10-10 10:00:13

@File Description: 

"""

import os
import sys
import logging
from time import strftime

# 输出日志路径
PATH = os.path.abspath('.') + '/logs/'

if os.path.exists(PATH) == False:
    os.makedirs(PATH)

# 设置日志格式#和时间格式
FMT = '%(asctime)s %(filename)s [line:%(lineno)d] %(levelname)s: %(message)s'
DATEFMT = '%Y-%m-%d %H:%M:%S'

class CLog(object):
    def __init__(self):
        self.logger = logging.getLogger()
        
           # 移除所有的旧处理器
        for handler in self.logger.handlers[:]:
            self.logger.removeHandler(handler)
        
        
        self.formatter = logging.Formatter(fmt=FMT, datefmt=DATEFMT)
        self.log_filename = '{0}{1}.log'.format(PATH, strftime("%Y-%m-%d"))

        self.logger.addHandler(self.get_file_handler(self.log_filename))
        # self.logger.addHandler(self.get_console_handler())
        
        # 设置日志的默认级别
        self.logger.setLevel(logging.DEBUG)

    # 输出到文件handler的函数定义
    def get_file_handler(self, filename):
        filehandler = logging.FileHandler(filename, encoding="utf-8")
        filehandler.setFormatter(self.formatter)
        return filehandler

    # 输出到控制台handler的函数定义
    def get_console_handler(self):
        console_handler = logging.StreamHandler(sys.stdout)
        console_handler.setFormatter(self.formatter)
        return console_handler
    
